package cn.jcby.app.lebu.controller;


import cn.jcby.app.lebu.controller.req.*;
import cn.jcby.app.lebu.controller.res.OtherUserInfoRes;
import cn.jcby.app.lebu.controller.res.UserLoginRes;
import cn.jcby.app.lebu.controller.res.UserNearByRes;
import cn.jcby.app.lebu.service.UserService;
import cn.jcby.app.lebu.utils.PageUtils;
import cn.jcby.app.lebu.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


/**
 * 个人信息表
 *
 * @author lht
 * @email 
 * @date 2023-12-07 15:13:35
 */
@RestController
@RequestMapping("/user")
@Api(tags="个人信息表")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation("登录")
    @ApiResponses({
          @ApiResponse(code = 200,message = "ok",response = UserLoginRes.class)
    })
    public Result login(@RequestBody @Validated LoginUserReq req, HttpServletResponse response){
        UserLoginRes userLogin = userService.login(req,response);
        return Result.ok().put("data", userLogin);
    }

    //获取验证码
    @GetMapping("/getVerifyCode")
    @ApiOperation("获取验证码")
    public Result getVerifyCode(@RequestParam("mobile") String mobile){
        return userService.getVerifyCode(mobile);
    }

    /**
     * 列表
     */
    /*@PostMapping("/list")
    @ApiOperation("列表")
    @ApiResponses({
          @ApiResponse(code = 200,message = "ok",response = UserEntity.class)
    })
    public Result list(@RequestBody BasicListReq params){
        PageUtils page = userService.queryPage(params);
        return Result.ok().put("data", page);
    }*/


    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    @ApiResponses({
         @ApiResponse(code = 200,message = "ok",response = UserLoginRes.class)
    })
    public Result info(){
        UserLoginRes user = userService.infoById();
        if (user == null){
            return Result.error("该记录不存在");
        }else {
            return Result.ok().put("data", user);
        }
    }

    @GetMapping("/otherInfo/{userId}")
    @ApiOperation("ta的详情")
    @ApiResponses({
            @ApiResponse(code = 200,message = "ok",response = OtherUserInfoRes.class)
    })
    public Result otherInfo(@PathVariable(value ="userId")String userId){
        OtherUserInfoRes user = userService.otherInfoById(userId);
        return Result.ok().put("data", user);
    }


    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation("修改")
    public Result update(@RequestBody UpdateUserReq req){
        userService.updateBy(req);
        return Result.ok();
    }

    //实名认证
    @PostMapping("/auth")
    @ApiOperation("实名认证")
    public Result auth(@RequestBody AuthUserReq req) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
        return userService.auth(req);
    }
    @PostMapping("/update/lastPoint")
    @ApiOperation("修改最近一次位置")
    public Result lastPoint(@RequestBody @Validated UserLastPointReq req){
        userService.updateLocation(req) ;
        return Result.ok();
    }


    @ApiOperation("附近列表")
    @PostMapping("/nearby")
    @ApiResponses({
          @ApiResponse(code = 200,message = "ok",response = UserNearByRes.class)
    })
    public Result nearby(@RequestBody UserNearByReq req){
        //默认显示该用户附近人信息
        PageUtils page= userService.nearBy(req) ;

        return Result.ok().put("data",page);
    }

    @ApiOperation("绑定openid")
    @PostMapping("/bindOpenid")
    public Result bindOpenid(@RequestParam(value ="openId")String openId){
        userService.bindOpenId(openId) ;
        return Result.ok();
    }

}
